package com.zhenyi.jiahua.index.admin;

import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhResource;
import com.jiahua.common.model.JhRole;
import com.jiahua.common.model.JhRoleResource;
import com.zhenyi.jiahua.index.JHController;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class RoleController extends JHController {

	@Override
	public void list() {
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindRole");
		Page<Record> pages = Db.use("jiahua").paginate(getPage(), PAGE_SIZE, sqlPara);
		setRainbow(pages);
		setItems(JSONUtil.toJsonStr(pages.getList()));
		render("/jh/admin/staff/role.html");
	}

	@Override
	public void info() {
		Long roleId = getParaToLong("roleId");
		if (roleId != null) {
			JhRole role = JhRole.dao.findById(roleId);
			if (role == null) {
				renderFailure(404, "role not found");
				return;
			}
			setInfoItem(role);
		}

		Kv kv = Kv.create();
		kv.set("role", roleId);
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHFindResource", kv);
		List<JhResource> list = JhResource.dao.find(sqlPara);
		setItems(JSONUtil.toJsonStr(list));

		// ArrayList<DBMap> arrayList = new ArrayList<>();
		SqlPara sqlParaRoot = Db.use("jiahua").getSqlPara("JHFindResourceTree", Kv.by("role", roleId));
		List<Record> items = Db.use("jiahua").find(sqlParaRoot);
		for (Record record : items) {
			Long rid = record.getLong("id");
			List<Record> children = getChildren(rid, roleId);
			if (children != null) {
				record.set("children", children);
			}
		}
		setAttr("data", JsonKit.toJson(items));
		render("/jh/admin/staff/roleInfo.html");
	}

	private List<Record> getChildren(Long resourceId, Long roleId) {
		SqlPara sqlParaRoot = Db.use("jiahua").getSqlPara("JHFindResourceTree",
				Kv.by("resourceId", resourceId).set("role", roleId));
		List<Record> items = Db.use("jiahua").find(sqlParaRoot);
		if (items.size() == 0) {
			return null;
		}
		for (Record record : items) {
			Long rid = record.getLong("id");
			List<Record> children = getChildren(rid, roleId);
			if (children != null) {
				record.set("children", children);
			}
		}
		return items;
	}

	@Override
	public void submit() {
		Long roleId = getParaToLong("roleId");
		String name = getPara("name");
		String json = getPara("json");
		try {
			JhRole role;
			if (roleId == null) {
				role = new JhRole();
				role.save();
				roleId = role.getId();
			} else {
				role = JhRole.dao.findById(roleId);
			}
			role.setName(name);
			role.update();
			JSONArray array = JSONUtil.parseArray(json);
			for (int i = 0; i < array.size(); i++) {
				JSONObject obj = array.getJSONObject(i);
				Long resourceId = obj.getLong("resourceId");
				JhRoleResource roleResource = JhRoleResource.dao.findById(resourceId, roleId);
				if (roleResource == null) {
					roleResource = new JhRoleResource();
					roleResource.setRoleId(roleId);
					roleResource.setResourceId(resourceId);
					roleResource.save();
				}
				Integer type = obj.getInt("type", 0);
				roleResource.setType(type);
				if (type.equals(0)) {
					/**
					 * 全部权限
					 */
					roleResource.setCanEdit(1);
					roleResource.setCanShow(1);
					roleResource.setCanNew(1);
				} else if (type.equals(1)) {
					/**
					 * 只读
					 */
					roleResource.setCanEdit(0);
					roleResource.setCanShow(1);
					roleResource.setCanNew(0);
				} else if (type.equals(2)) {
					/**
					 * 不可见
					 */
					roleResource.setCanEdit(0);
					roleResource.setCanShow(0);
					roleResource.setCanNew(0);
				} else if (type.equals(3)) {
					/**
					 * 可以新增,只读
					 */
					roleResource.setCanEdit(0);
					roleResource.setCanShow(1);
					roleResource.setCanNew(1);
				} else {
					/**
					 * 无权限
					 */
					roleResource.setCanEdit(0);
					roleResource.setCanShow(0);
					roleResource.setCanNew(0);
				}
				roleResource.update();
			}
			renderSuccess();
		} catch (Exception e) {
			System.out.println(json);
			renderFailure();
		}
	}

	public void delete() {
		Long roleId = getParaToLong("roleId");
		if (roleId == null) {
			renderFailure(500, "id is null");
			return;
		}
		JhRole info = JhRole.dao.findById(roleId);
		if (info == null) {
			renderFailure(404, "role not found");
			return;
		}
		renderFlag(info.delete());
	}
}
